package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddCache;
import com.baizhi.annotation.AddLog;
import com.baizhi.annotation.DelCache;
import com.baizhi.dao.CategoryMapper;
import com.baizhi.entity.Admin;
import com.baizhi.entity.AdminExample;
import com.baizhi.entity.Category;
import com.baizhi.entity.CategoryExample;
import com.baizhi.service.CategoryService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service("categoryService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @AddCache
    @Override
    public HashMap<String, Object> queryOneCatePage(Integer page, Integer rows) {
        HashMap<String, Object> map = new HashMap<>();
        //创建Category的条件查询条件实体
        CategoryExample example = new CategoryExample();
        example.createCriteria().andLevelsEqualTo(1);
        map.put("page",page);
        //获取行数
        int records = categoryMapper.selectCountByExample(example);
        map.put("records",records);
        //总页数
        int total = records%rows==0?records/rows:records/rows+1;
        map.put("total",total);
        //分页数据
        RowBounds rowBounds = new RowBounds((page-1)*rows,rows);
        List<Category> categories = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);
        map.put("rows",categories);
        return map;
    }

    @AddCache
    @Override
    public HashMap<String, Object> queryTwoCatePage(Integer page, Integer rows, String parentId) {
        HashMap<String, Object> map = new HashMap<>();
        //创建Category的条件查询条件实体
        CategoryExample example = new CategoryExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        map.put("page",page);
        //获取行数
        int records = categoryMapper.selectCountByExample(example);
        map.put("records",records);
        //总页数
        int total = records%rows==0?records/rows:records/rows+1;
        map.put("total",total);
        //分页数据
        RowBounds rowBounds = new RowBounds((page-1)*rows,rows);
        List<Category> categories = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);
        map.put("rows",categories);
        return map;
    }


    @DelCache
    @AddLog(description = "修改类别")
    @Override
    @Transactional
    public void update(Category category) {
        categoryMapper.updateByPrimaryKeySelective(category);
    }


    @DelCache
    @AddLog(description = "添加类别")
    @Override
    @Transactional
    public void add(Category category) {
        System.out.println(category+",");
        category.setId(UUID.randomUUID().toString());
        categoryMapper.insertSelective(category);
    }


    @DelCache
    @AddLog(description = "删除类别")
    @Override
    @Transactional
    public HashMap<String ,Object> del(Category category) {
        //categoryMapper.selectOneByExample();
        HashMap<String, Object> map = new HashMap<>();
        /*
            判断当前类别是一级类别还是二级类别
            a：一级类别
                判断是否有子类别
            b：二级类别
                判断是否有用户发布的相关动态
        */
        if(category.getLevels() == 1){
            CategoryExample example = new CategoryExample();
            //根据传入的id去查parent_id是否有数据
            example.createCriteria().andParentIdEqualTo(category.getId());
            List<Category> categories = categoryMapper.selectByExample(example);
            //如果为空那么就是没有子类别的一级类别
            //可直接删除
            if(categories.isEmpty()){
                categoryMapper.delete(category);
                map.put("status","100");
                map.put("message","删除成功");
            }else{
                //如果不为空那么就是有子类别的一级类别
                map.put("status","101");
                map.put("message","删除失败，当前一级类别下有子类别");
            }
        }else{
            //============后续判断是否有用户发布的相关动态
            //在此先直接删除
            categoryMapper.delete(category);
            map.put("status","100");
            map.put("message","删除成功");

        }
        return map;
    }
}
